﻿/*
 * DotNetCrypt - an open source library of cryptographic algorithms for .NET
 * Copyright (C) 2009 David Musgrove
 * 
 * This file is part of DotNetCrypt.
 *
 * DotNetCrypt is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * DotNetCrypt is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

using System;
using System.Security.Cryptography;

namespace DotNetCrypt
{
    /// <summary>
    /// Performs a cryptographic transformation of data using the
    /// <see cref="TripleDES" /> algorithm. This class cannot be inherited.
    /// </summary>
// ReSharper disable InconsistentNaming
    public sealed class TripleDESManagedTransform : ManagedTransformBase
// ReSharper restore InconsistentNaming
    {
        private readonly byte[] _key;
        private DESManagedTransform[] _des;

        #region S-box arrays

        private static readonly uint[] _sBox1 = {
                                                0x01010400, 0x00000000, 0x00010000, 0x01010404,
                                                0x01010004, 0x00010404, 0x00000004, 0x00010000,
                                                0x00000400, 0x01010400, 0x01010404, 0x00000400,
                                                0x01000404, 0x01010004, 0x01000000, 0x00000004,
                                                0x00000404, 0x01000400, 0x01000400, 0x00010400,
                                                0x00010400, 0x01010000, 0x01010000, 0x01000404,
                                                0x00010004, 0x01000004, 0x01000004, 0x00010004,
                                                0x00000000, 0x00000404, 0x00010404, 0x01000000,
                                                0x00010000, 0x01010404, 0x00000004, 0x01010000,
                                                0x01010400, 0x01000000, 0x01000000, 0x00000400,
                                                0x01010004, 0x00010000, 0x00010400, 0x01000004,
                                                0x00000400, 0x00000004, 0x01000404, 0x00010404,
                                                0x01010404, 0x00010004, 0x01010000, 0x01000404,
                                                0x01000004, 0x00000404, 0x00010404, 0x01010400,
                                                0x00000404, 0x01000400, 0x01000400, 0x00000000,
                                                0x00010004, 0x00010400, 0x00000000, 0x01010004
                                };

        private static readonly uint[] _sBox2 = {
                                                0x80108020, 0x80008000, 0x00008000, 0x00108020,
                                                0x00100000, 0x00000020, 0x80100020, 0x80008020,
                                                0x80000020, 0x80108020, 0x80108000, 0x80000000,
                                                0x80008000, 0x00100000, 0x00000020, 0x80100020,
                                                0x00108000, 0x00100020, 0x80008020, 0x00000000,
                                                0x80000000, 0x00008000, 0x00108020, 0x80100000,
                                                0x00100020, 0x80000020, 0x00000000, 0x00108000,
                                                0x00008020, 0x80108000, 0x80100000, 0x00008020,
                                                0x00000000, 0x00108020, 0x80100020, 0x00100000,
                                                0x80008020, 0x80100000, 0x80108000, 0x00008000,
                                                0x80100000, 0x80008000, 0x00000020, 0x80108020,
                                                0x00108020, 0x00000020, 0x00008000, 0x80000000,
                                                0x00008020, 0x80108000, 0x00100000, 0x80000020,
                                                0x00100020, 0x80008020, 0x80000020, 0x00100020,
                                                0x00108000, 0x00000000, 0x80008000, 0x00008020,
                                                0x80000000, 0x80100020, 0x80108020, 0x00108000
                                };

        private static readonly uint[] _sBox3 = {
                                                0x00000208, 0x08020200, 0x00000000, 0x08020008,
                                                0x08000200, 0x00000000, 0x00020208, 0x08000200,
                                                0x00020008, 0x08000008, 0x08000008, 0x00020000,
                                                0x08020208, 0x00020008, 0x08020000, 0x00000208,
                                                0x08000000, 0x00000008, 0x08020200, 0x00000200,
                                                0x00020200, 0x08020000, 0x08020008, 0x00020208,
                                                0x08000208, 0x00020200, 0x00020000, 0x08000208,
                                                0x00000008, 0x08020208, 0x00000200, 0x08000000,
                                                0x08020200, 0x08000000, 0x00020008, 0x00000208,
                                                0x00020000, 0x08020200, 0x08000200, 0x00000000,
                                                0x00000200, 0x00020008, 0x08020208, 0x08000200,
                                                0x08000008, 0x00000200, 0x00000000, 0x08020008,
                                                0x08000208, 0x00020000, 0x08000000, 0x08020208,
                                                0x00000008, 0x00020208, 0x00020200, 0x08000008,
                                                0x08020000, 0x08000208, 0x00000208, 0x08020000,
                                                0x00020208, 0x00000008, 0x08020008, 0x00020200
                                };

        private static readonly uint[] _sBox4 = {
                                                0x00802001, 0x00002081, 0x00002081, 0x00000080,
                                                0x00802080, 0x00800081, 0x00800001, 0x00002001,
                                                0x00000000, 0x00802000, 0x00802000, 0x00802081,
                                                0x00000081, 0x00000000, 0x00800080, 0x00800001,
                                                0x00000001, 0x00002000, 0x00800000, 0x00802001,
                                                0x00000080, 0x00800000, 0x00002001, 0x00002080,
                                                0x00800081, 0x00000001, 0x00002080, 0x00800080,
                                                0x00002000, 0x00802080, 0x00802081, 0x00000081,
                                                0x00800080, 0x00800001, 0x00802000, 0x00802081,
                                                0x00000081, 0x00000000, 0x00000000, 0x00802000,
                                                0x00002080, 0x00800080, 0x00800081, 0x00000001,
                                                0x00802001, 0x00002081, 0x00002081, 0x00000080,
                                                0x00802081, 0x00000081, 0x00000001, 0x00002000,
                                                0x00800001, 0x00002001, 0x00802080, 0x00800081,
                                                0x00002001, 0x00002080, 0x00800000, 0x00802001,
                                                0x00000080, 0x00800000, 0x00002000, 0x00802080
                                };

        private static readonly uint[] _sBox5 = {
                                                0x00000100, 0x02080100, 0x02080000, 0x42000100,
                                                0x00080000, 0x00000100, 0x40000000, 0x02080000,
                                                0x40080100, 0x00080000, 0x02000100, 0x40080100,
                                                0x42000100, 0x42080000, 0x00080100, 0x40000000,
                                                0x02000000, 0x40080000, 0x40080000, 0x00000000,
                                                0x40000100, 0x42080100, 0x42080100, 0x02000100,
                                                0x42080000, 0x40000100, 0x00000000, 0x42000000,
                                                0x02080100, 0x02000000, 0x42000000, 0x00080100,
                                                0x00080000, 0x42000100, 0x00000100, 0x02000000,
                                                0x40000000, 0x02080000, 0x42000100, 0x40080100,
                                                0x02000100, 0x40000000, 0x42080000, 0x02080100,
                                                0x40080100, 0x00000100, 0x02000000, 0x42080000,
                                                0x42080100, 0x00080100, 0x42000000, 0x42080100,
                                                0x02080000, 0x00000000, 0x40080000, 0x42000000,
                                                0x00080100, 0x02000100, 0x40000100, 0x00080000,
                                                0x00000000, 0x40080000, 0x02080100, 0x40000100
                                };

        private static readonly uint[] _sBox6 = {
                                                0x20000010, 0x20400000, 0x00004000, 0x20404010,
                                                0x20400000, 0x00000010, 0x20404010, 0x00400000,
                                                0x20004000, 0x00404010, 0x00400000, 0x20000010,
                                                0x00400010, 0x20004000, 0x20000000, 0x00004010,
                                                0x00000000, 0x00400010, 0x20004010, 0x00004000,
                                                0x00404000, 0x20004010, 0x00000010, 0x20400010,
                                                0x20400010, 0x00000000, 0x00404010, 0x20404000,
                                                0x00004010, 0x00404000, 0x20404000, 0x20000000,
                                                0x20004000, 0x00000010, 0x20400010, 0x00404000,
                                                0x20404010, 0x00400000, 0x00004010, 0x20000010,
                                                0x00400000, 0x20004000, 0x20000000, 0x00004010,
                                                0x20000010, 0x20404010, 0x00404000, 0x20400000,
                                                0x00404010, 0x20404000, 0x00000000, 0x20400010,
                                                0x00000010, 0x00004000, 0x20400000, 0x00404010,
                                                0x00004000, 0x00400010, 0x20004010, 0x00000000,
                                                0x20404000, 0x20000000, 0x00400010, 0x20004010
                                };

        private static readonly uint[] _sBox7 = {
                                                0x00200000, 0x04200002, 0x04000802, 0x00000000,
                                                0x00000800, 0x04000802, 0x00200802, 0x04200800,
                                                0x04200802, 0x00200000, 0x00000000, 0x04000002,
                                                0x00000002, 0x04000000, 0x04200002, 0x00000802,
                                                0x04000800, 0x00200802, 0x00200002, 0x04000800,
                                                0x04000002, 0x04200000, 0x04200800, 0x00200002,
                                                0x04200000, 0x00000800, 0x00000802, 0x04200802,
                                                0x00200800, 0x00000002, 0x04000000, 0x00200800,
                                                0x04000000, 0x00200800, 0x00200000, 0x04000802,
                                                0x04000802, 0x04200002, 0x04200002, 0x00000002,
                                                0x00200002, 0x04000000, 0x04000800, 0x00200000,
                                                0x04200800, 0x00000802, 0x00200802, 0x04200800,
                                                0x00000802, 0x04000002, 0x04200802, 0x04200000,
                                                0x00200800, 0x00000000, 0x00000002, 0x04200802,
                                                0x00000000, 0x00200802, 0x04200000, 0x00000800,
                                                0x04000002, 0x04000800, 0x00000800, 0x00200002
                                };

        private static readonly uint[] _sBox8 = {
                                                0x10001040, 0x00001000, 0x00040000, 0x10041040,
                                                0x10000000, 0x10001040, 0x00000040, 0x10000000,
                                                0x00040040, 0x10040000, 0x10041040, 0x00041000,
                                                0x10041000, 0x00041040, 0x00001000, 0x00000040,
                                                0x10040000, 0x10000040, 0x10001000, 0x00001040,
                                                0x00041000, 0x00040040, 0x10040040, 0x10041000,
                                                0x00001040, 0x00000000, 0x00000000, 0x10040040,
                                                0x10000040, 0x10001000, 0x00041040, 0x00040000,
                                                0x00041040, 0x00040000, 0x10041000, 0x00001000,
                                                0x00000040, 0x10040040, 0x00001000, 0x00041040,
                                                0x10001000, 0x00000040, 0x10000040, 0x10040000,
                                                0x10040040, 0x10000000, 0x00040000, 0x10001040,
                                                0x00000000, 0x10041040, 0x00040040, 0x10000040,
                                                0x10040000, 0x10001000, 0x10001040, 0x00000000,
                                                0x10041040, 0x00041000, 0x00041000, 0x00001040,
                                                0x00001040, 0x00040040, 0x10000000, 0x10041000
                                };

        private static readonly uint[] _leftHalfSwap = {
                                                0x00000000, 0x00000001, 0x00000100, 0x00000101,
                                                0x00010000, 0x00010001, 0x00010100, 0x00010101,
                                                0x01000000, 0x01000001, 0x01000100, 0x01000101,
                                                0x01010000, 0x01010001, 0x01010100, 0x01010101
                                };

        private static readonly uint[] _rightHalfSwap = {
                                                0x00000000, 0x01000000, 0x00010000, 0x01010000,
                                                0x00000100, 0x01000100, 0x00010100, 0x01010100,
                                                0x00000001, 0x01000001, 0x00010001, 0x01010001,
                                                0x00000101, 0x01000101, 0x00010101, 0x01010101
                                };

        #endregion

        internal TripleDESManagedTransform(ISymmetricAlgorithm algorithm, byte[] rgbKey, byte[] rgbIv, TransformDirection transformDirection)
            : base(algorithm, rgbIv, transformDirection, Endianness.Big)
        {
            _key = new byte[24];
            rgbKey.CopyTo(_key, 0);
            if (rgbKey.Length <= 16)
            {
                for (int i = 0; i < 8; i++) _key[i + 16] = _key[i];
            }
            if (rgbKey.Length <= 8)
            {
                rgbKey.CopyTo(_key, 8);
            }
            AssignKeys();
        }

        private void AssignKeys()
        {
            _des = new DESManagedTransform[3];
            var des = new DESManaged();
            var key = new byte[8];
            for (int i = 0; i < 8; i++) key[i] = _key[i];
            _des[0] = new DESManagedTransform(des, key, null, TransformDirection.Encrypt);
            for (int i = 0; i < 8; i++) key[i] = _key[i + 8];
            _des[1] = new DESManagedTransform(des, key, null, TransformDirection.Encrypt);
            for (int i = 0; i < 8; i++) key[i] = _key[i + 16];
            _des[2] = new DESManagedTransform(des, key, null, TransformDirection.Encrypt);
        }

        /// <summary>
        /// Performs the encryption transformation on a block of bytes that
        /// have been translated into words using the big endian convention.
        /// </summary>
        /// <param name="plain">
        /// The words to encrypt.
        /// </param>
        [CLSCompliant(false)]
        protected internal override void Encrypt(uint[] plain)
        {
            _des[0].Encrypt(plain);
            _des[1].Decrypt(plain);
            _des[2].Encrypt(plain);
        }

        /// <summary>
        /// Performs the decryption transformation on a block of bytes that
        /// have been translated into words using the big endian convention.
        /// </summary>
        /// <param name="cipher">
        /// The words to decrypt.
        /// </param>
        [CLSCompliant(false)]
        protected internal override void Decrypt(uint[] cipher)
        {
            _des[0].Decrypt(cipher);
            _des[1].Encrypt(cipher);
            _des[2].Decrypt(cipher);
        }

        /// <summary>
        /// Clears all potentially sensitive data stores.
        /// </summary>
        protected internal override void Reset()
        {
            if (_des != null)
            {
                foreach (DESManagedTransform des in _des)
                {
                    des.Reset();
                }
            }
            if (_key != null)
            {
                Array.Clear(_key, 0, _key.Length);
            }
            base.Reset();
        }
    }
}
